package com.chuangjiangx.member.basic.ddd.application;

import com.chuangjiangx.member.basic.ddd.application.command.AddAndModifyMbrConfigCommand;
import com.chuangjiangx.member.stored.ddd.application.command.ControlMerchantStoredFuncCommand;
import com.chuangjiangx.member.coupon.ddd.domain.model.MbrConfig;
import com.chuangjiangx.member.coupon.ddd.domain.repository.MbrConfigRepository;
import com.chuangjiangx.member.basic.ddd.domain.model.CustomRecharge;
import com.chuangjiangx.member.basic.ddd.domain.model.MerchantId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author cj-ch
 * @date 2018/6/29 下午12:14
 */
@Service
public class MbrConfigApplication {

    @Autowired
    private MbrConfigRepository mbrConfigRepository;

    public void addAndmodify(AddAndModifyMbrConfigCommand command){
        MerchantId merchantId = new MerchantId(command.getMerchantId());
        MbrConfig mbrConfig = mbrConfigRepository.findByMerchantId(merchantId);
        if(null == mbrConfig){
            mbrConfig = new MbrConfig(
                    null,
                    merchantId,
                    null == command.getCustomRecharge() ? CustomRecharge.OPEND : CustomRecharge.getCustomRecharge(command.getCustomRecharge()),
                    command.getSubscribePnGiftScore(),
                    command.getCardConsumerGrantScore(),
                    new Date(),
                    null
            );
        } else {
            if(null != command.getCustomRecharge()){
                mbrConfig.changeCustomStored(CustomRecharge.getCustomRecharge(command.getCustomRecharge()));
            }
            if(null != command.getSubscribePnGiftScore()){
                mbrConfig.changeSubscribePnGiftScore(command.getSubscribePnGiftScore());
            }
            if(null != command.getCardConsumerGrantScore()){
                mbrConfig.changeCardConsumerGrantScore(command.getCardConsumerGrantScore());
            }
        }
        if(null == mbrConfig.getId()){
            mbrConfigRepository.save(mbrConfig);
        }else{
            mbrConfigRepository.update(mbrConfig);
        }
    }

    /**
     * 控制商户的储值功能
     * @param command
     */
    public void controlMerchantStoredFunc(ControlMerchantStoredFuncCommand command){
        MerchantId merchantId = new MerchantId(command.getMerchantId());
        MbrConfig mbrConfig = mbrConfigRepository.findByMerchantId(merchantId);
        if(null == mbrConfig){
            mbrConfig = new MbrConfig(
                    null,
                    merchantId,
                    null ,
                    null,
                    null,
                    new Date(),
                    null
            );
        }
        mbrConfig.controlStoredFunc(command.getStatus());
        if(null == mbrConfig.getId()){
            mbrConfigRepository.save(mbrConfig);
        }else{
            mbrConfigRepository.update(mbrConfig);
        }
    }
}
